<?php
/**
 *
 * @author griever
 * @version
 */
require_once 'Base.php';
require_once 'models/SearchAgents.php';
require_once 'ArrayUtils.php';

/**
 * GetSearchResult Action Helper
 *
 * @uses actionHelper Custom_Helper
 */
class Custom_Helper_GetSearchResult extends Dummy_Helper_Base {
	
	public function prepareResult($allow, $searchExecutor, $query, $start, $rows, $boostFunction, $core, $handler) {
//		
//		if (! $this->hasInputParam ( 'search-query' )) {
//			throw new Zend_Exception ( Message::QUERY_MUST_DEFINED );
//		}
		
		if ($searchExecutor == null)	{
//			$searchExecutor = new DirectSolrSearchExecutor();
			$searchExecutor = new AgentMediatorSearchExecutor();
		}
		
//		$query = $this->getRequest ()->getParam ( 'search-query' );
//		$page = $this->getRequest ()->getParam ( 'search-page' );
//		$numRes = $this->getRequest ()->getParam ( 'num-res' );
		
		$page = $start;
		$numRes = $rows;
		
		if ($page == null){
			$page = 1;
		}
		
		//Get search display types
		$field_list = $this->getRequest ()->getParam ( 'search_display_type' );
		
		//-----------------------------------ANDY---------------------------------
		
		
		//		$type = $field_list;
		$query = mb_strtolower ( $query ,"UTF-8");
		$query = trim ( $query );
		$query = preg_replace ( "/[:]+/", ":", $query );
		$query = preg_replace ( "/\+\+/", "\+\+", $query );
		$query = ArrayUtils::solrEscape ( $query, $allow );
		$query = str_replace ( " or ", " OR ", $query );
		$query = str_replace ( " and ", " AND ", $query );
		$queryStr = $query;
		$search = new ProfileSearch ( $searchExecutor, $queryStr, $page, $numRes, $boostFunction, $core, $handler );
		return $search;
	}
	
	/**
	 * Strategy pattern: call helper as broker method
	 */
	public function direct($allow = array('+', '"'), $searchExecutor = null, $query, $start, $rows, $boostFunction = "", $core = "knowledge", $handler = "notification") {
		// TODO Auto-generated 'direct' method
		return $this->prepareResult ( $allow, $searchExecutor, $query, $start, $rows, $boostFunction, $core, $handler );
	}

}